package cn.aohan.common.dto;


/**
 * 结果
 *
 * @author 傲寒
 * @since 2024/03/20
 */
public class Result<E> {

    private String code;

    private String message;

    private E data;


    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public E getData() {
        return data;
    }

    protected Result() {
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    protected Result(String code, String message, E data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private Result(String code, String message) {
        this(code, message, null);
    }

    public boolean isSuccess() {
        return ResultCodeEnum.SUCCESS.getCode().equals(code);
    }

    public static <T> Result<T> success() {
        return of(ResultCodeEnum.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return of(ResultCodeEnum.SUCCESS, data);
    }

    public static <T> Result<T> error() {
        return of(ResultCodeEnum.ERROR);
    }

    public static <T> Result<T> of(String code, String msg) {
        return new Result<>(code, msg, null);
    }

    public static <T> Result<T> of(String code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> of(ResultCodeEnum resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), null);
    }

    private static <T> Result<T> of(T data) {
        return of(ResultCodeEnum.SUCCESS, data);
    }

    private static <T> Result<T> of(ResultCodeEnum resultCode, T data) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), data);
    }

}
